package com.zhiyou100.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Drug;
import com.zhiyou100.service.DrugService;

@Controller
public class DrugController {
	@Autowired
	private DrugService drugService;
	// 查询
	@RequestMapping("/drug/list")
	public String listDrug(Model model,
			@RequestParam(defaultValue="1")int pageNo,
			@RequestParam(defaultValue="")String drugName,
			@RequestParam(defaultValue="")String drugType) {
		// 模糊查询
		Map<String,Object> map = new HashMap<>();
		map.put("drugName", drugName);
		map.put("drugType", drugType);
		// 分页
		PageHelper.startPage(pageNo, 2);
		// 调用服务层
		List<Drug> drug = drugService.findAll(map);
		System.out.println("查询所有drug: "+drug);
		PageInfo<Drug> pageInfo = new PageInfo<>(drug);
		model.addAttribute("drug", pageInfo.getList());
		model.addAttribute("map", map);
		model.addAttribute("page", pageInfo);
		return "forward:/view/medicine/drugList.jsp";
	}
	// 详情
	@RequestMapping("/drug/detail")
	public String detailDrug(String drug_num,Model model) {
		Drug drug = drugService.detailById(drug_num);
		model.addAttribute("drug", drug);
		return "forward:/view/medicine/drugDetail.jsp";
	}
	// 添加
	@RequestMapping(value="/drug/add",method=RequestMethod.GET)
	public String addDrug() {
		return "forward:/view/medicine/drugAdd.jsp";
	}
	@RequestMapping(value="/drug/add",method=RequestMethod.POST)
	public String addDrug2(Drug drug,HttpServletRequest req,@RequestParam("drugUrl")MultipartFile drugUrl){
		// 获取最终上传的位置
		String realPath = req.getServletContext().getRealPath("/upload");
		System.out.println("realPath: "+realPath);
		// 创建新的文件
		File file = new File(realPath);
		if(!file.exists()) {
			file.mkdir();
		}
		String fileName = drugUrl.getOriginalFilename();
		String[] split = fileName.split("\\.");
		// 获得后缀
		String suffix = split[1];
		long prefix = new Date().getTime();
		String newFileName = prefix+"."+suffix;
		File newFile = new File(file,newFileName);
		// 上传文件
		try {
			FileUtils.writeByteArrayToFile(newFile, drugUrl.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
		drug.setDrug_url(newFile.getName());
		System.out.println("drug"+drug);
		drugService.addDrug(drug);
		return "forward:/drug/list";
	}
	
	// 更新
	@RequestMapping("/drug/edit")
	public String editDrug(String drug_num,Model model) {
		Drug drug = drugService.findToEdit(drug_num);
		model.addAttribute("drug", drug);
		return "forward:/view/medicine/drugEdit.jsp";
	}
	@RequestMapping("/drug/update")
	public String updateDrug(Drug drug,HttpServletRequest req,@RequestParam("drugUrl")MultipartFile drugUrl) {
		// 获取最终上传的位置
		String realPath = req.getServletContext().getRealPath("/upload");
		System.out.println("realPath: "+realPath);
		// 创建新的文件
		File file = new File(realPath);
		if(!file.exists()) {
			file.mkdir();
		}
		String fileName = drugUrl.getOriginalFilename();
		String[] split = fileName.split("\\.");
		// 获得后缀
		String suffix = split[1];
		long prefix = new Date().getTime();
		String newFileName = prefix+"."+suffix;
		File newFile = new File(file,newFileName);
		// 上传文件
		try {
			FileUtils.writeByteArrayToFile(newFile, drugUrl.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
		drug.setDrug_url(newFile.getName());
		System.out.println("drug"+drug);
		drugService.updateById(drug);
		System.out.println("更新时从页面接收到的drug: "+drug);
		return "forward:/drug/list";
	}
	
	// 文件上传
	@RequestMapping("/drug/file")
	public String drugFile(HttpServletRequest req,@RequestParam("drug_url")MultipartFile drug_url) throws IOException {
		// 获取最终上传的位置
		String realPath = req.getServletContext().getRealPath("/upload");
		System.out.println("realPath: "+realPath);
		// 创建新的文件
		File file = new File(realPath);
		if(!file.exists()) {
			file.mkdir();
		}
		String fileName = drug_url.getOriginalFilename();
		String[] split = fileName.split("\\.");
		// 获得后缀
		String suffix = split[1];
		long prefix = new Date().getTime();
		String newFileName = prefix+"."+suffix;
		File newFile = new File(file,newFileName);
		// 上传文件
		FileUtils.writeByteArrayToFile(newFile, drug_url.getBytes());
		req.setAttribute("path", "/upload/"+newFileName);
		return "forward:/view/medicine/drugAdd.jsp";
	}
}
